package com.system.boot.setting.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.api.SuperInterface;
import com.system.api.setting.entity.dto.IconFormDto;
import com.system.api.setting.entity.po.SysIcon;
import com.system.api.setting.entity.search.IconSearch;
import com.system.boot.setting.converter.IconConverter;
import com.system.boot.setting.service.IconService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.yang.common.core.exception.BaseException;
import org.yang.common.core.result.BaseController;
import org.yang.common.core.result.R;
import org.yang.database.orm.entity.BaseEnabled;
import org.yang.database.orm.entity.BaseQuery;

import java.util.*;

import static org.yang.common.i18n.constant.I18nMessageConstant.VALIDATION_ID_EMPTY;

/**
 * 图标接口
 *
 * @author: lslands
 * @version: 1.0
 * @create: 2024/5/16
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/icon")
@RequiredArgsConstructor
public class IconController extends BaseController {

    private final IconService iconService;
    private final IconConverter iconConverter;
    private final SuperInterface<SysIcon> superInterface;

    /**
     * 新增/修改
     * @param dto 新增对象
     * @apiNote 通过是否存在主键,判定是新增还是修改;id为空则为新增，反之;
     * @author lslands
     * @date 2024/5/18 23:06
     * @return R<void>
     */
    @PostMapping("/saveOrUpdate")
    public R save(@RequestBody IconFormDto dto) throws BaseException {
        SysIcon record = iconConverter.formEntity(dto);
        return Objects.isNull(dto.getId())
                ? toCreateAjax(superInterface.handInsert(record))
                : toUpdateAjax(superInterface.handUpdateById(record));
    }

    /**
     * 更新状态
     * @param record 更新状态对象
     * @return R<void>
     */
    @PatchMapping("/updateStatus")
    public R updateStatus(@RequestBody BaseEnabled record) throws BaseException {
        return toUpdateAjax(
                superInterface.handEnabledById(record.getId(), record.getEnabled(), record.getRevision()));
    }

    /**
     * 单条数据查询
     * @param id 主键ID|1
     * @author lslands
     * @date 2024/5/19 23:05
     * @return SysIcon
     */
    @GetMapping("/getById")
    public R<SysIcon> getById(@RequestParam Long id){
        return R.success(iconService.getById(id));
    }

    /**
     * 分页模糊查询列表
     * @param search 模糊查询对象
     * @return R<Page<SysIcon>>
     */
    @GetMapping("/page")
    public R<Page<SysIcon>> page(@ModelAttribute IconSearch search) throws BaseException {
        BaseQuery baseQuery = iconConverter.queryEntity(search);
        SysIcon sysIcon = iconConverter.queryPoEntity(search);
        return R.success(superInterface.getPage(baseQuery, sysIcon));
    }

    /**
     * 单条逻辑删除
     * @param id 主键ID|1
     * @author lslands
     * @date 2024/5/19 22:28
     */
    @DeleteMapping("/deleteOne")
    public R deleteOne(@RequestBody Long id) throws BaseException {
        if(null == id){
            return R.fail(VALIDATION_ID_EMPTY);
        }
        return toDeleteAjax(superInterface.deleteOne(id));
    }

    /**
     * 批量删除
     * @param ids 主键ID集|[1,2]
     * @param delete 是否物理删除标记|false
     * @author lslands
     * @date 2024/5/19 22:29
     */
    @DeleteMapping("/deleteBatch/{delete}")
    public R deleteBatch(@RequestBody List<Long> ids,
                         @PathVariable("delete") Boolean delete) throws BaseException {
        return toDeleteAjax(superInterface.deleteBatch(delete,ids));

    }

}
